<?php

// +----------------------------------------------------------------------
// | H1CMS © OpenSource CMS
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2016 http://www.h1cms.com All rights reserved.
// | Copyright (c) 2014-2016 嘉兴领格信息技术有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Allen <allen@lg4.cn>
// +----------------------------------------------------------------------

namespace Lg\Database;

/**
 * Description of DB  
 * 
 * Exec.
 * @method static bool exec($query) PDO exec.
 * 
 * @author Allen <allen@lg4.cn>
 */
class DB {

    private function __construct() {
        
    }

    private function __destruct() {
        
    }

    private function __clone() {
        
    }

    /**
     * 
     * @return \Lg\Database\Database
     */
    public static function getConnection($target = NULL) {
        return Database::getConnection($target);
    }

    public static function __callStatic($name, $arguments) {
        return call_user_func_array([Database::getConnection(), $name], $arguments);
    }

    /**
     * 
     * @param type $target
     * @return \Lg\Database\Database
     */
    public static function connection($target = null) {
        return Database::getConnection($target);
    }

    /**
     * 
     * @param string $query
     * @param array $params
     * @return \PDOStatement
     */
    public static function query($query, $params = []) {
        return Database::getConnection()->query($query, $params);
    }

    /**
     * @return \PDO
     */
    public static function getPdo() {
        return Database::getConnection()->pdo();
    }

    /**
     * 更新表
     * @param string $table 表名称
     * @param array $columns 列
     * @param string|array $conditions 条件
     * @param array $params 参数
     * @return rowcount 更新行数
     */
    public static function update($table, $columns, $conditions = '', $params = array()) {
        return Database::getConnection()->update($table, $columns, $conditions, $params);
    }

    /**
     * 
     * @param string $table
     * @param type $conditions
     * @param type $params
     * @return type
     */
    public static function delete($table, $conditions = '', $params = []) {
        return Database::getConnection()->delete($table, $conditions, $params);
    }

    /**
     * 
     * @param string $table
     * @param array $columns
     * @return lastId
     */
    public static function insert($table, $columns) {
        return Database::getConnection()->insert($table, $columns);
    }

    /**
     * 获取单列 Array
     * @param string $query
     * @param array $params
     * @return mixed
     */
    public static function getCol($query, $params) {
        return Database::getConnection()->getCol($query, $params);
    }

    /**
     * 获取单个值
     * @param type $query
     * @param type $params
     * @return mixed
     */
    public static function getValue($query, $params = array()) {
        return Database::getConnection()->getValue($query, $params);
    }

    /**
     * 获取单行
     * @param type $query
     * @param type $params
     * @param type $fetch_style
     * @return type
     */
    public static function getRow($query, $params = [], $fetch_style = PDO::FETCH_OBJ) {
        return Database::getConnection()->getRow($query, $params, $fetch_style);
    }

    /**
     * 获取所有数据
     * @param type $query
     * @param type $params
     * @param type $fetch_style
     * @return type
     */
    public static function getAll($query, $params = [], $fetch_style = PDO::FETCH_OBJ) {
        return Database::getConnection()->getAll($query, $params, $fetch_style);
    }

    public static function lastId($name = null) {
        return Database::getConnection()->lastId($name);
    }

    public static function transaction($callable) {
        return Database::getConnection()->transaction($callable);
    }

}
